const app = getApp();
const {AskPage} = app._require("common/index");
const formMixin = app._require("mixins/form.mixin.js").default;
AskPage({

    /**
     * 页面的初始数据
     */
    data: {
        navBarHeight: app.globalData.navBarHeight,//导航栏高度
        undoItmes: [
            {
                "title": "任务待办",
                "tips": '待填报的资源采集任务',
                "icon": "/assets/home/tianbao.png",
                "path": "/pages/task-home/index"
            },
            {
                "title": "任务审批",
                "tips": '需要你审批的采集任务',
                "icon": "/assets/home/shenpi.png",
                "path": "/pages/task-home/index"
            },
            {
                "title": "抄送我的",
                "tips": '希望你了解的采集任务',
                "icon": "/assets/home/chaosong.png",
                "path": "/pages/task-copy/index"
            }
        ],
        // categoryItems:["日常办公","企业办事","村民办事","第三方"],
        // categoryItems: [],
        values: ["日常巡查", "执法", "养护", "调节"],
        undoDescItmes: ["0个", "0个", "0个"],
        banner: [{"path": "https://tianzuoyun-pro-1304386495.cos.ap-chengdu.myqcloud.com/wx/img/2021-11-23/wx_home_banner_1.png"},
            {"path": "https://tianzuoyun-pro-1304386495.cos.ap-chengdu.myqcloud.com/wx/img/2021-11-23/wx_home_banner_2.png"},
            {"path": "https://tianzuoyun-pro-1304386495.cos.ap-chengdu.myqcloud.com/wx/img/2021-11-23/wx_home_banner_3.png"}],
        clickIndex: 0,
        user: {
            userInfo: {
                orgCategoryId: ''
            }
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */

    onShow() {
        this.setData({
            user: app.user,
        });
        if (app.user.token.length == 0) {
            //清理数据
            this.setData({
                undoDescItmes: [],
                // categoryItems: null
            })
            return;
        }
        //检查微信绑定
        this.checkShowAlert();

        //刷新常用
        var value = wx.getStorageSync("common_use") || []
        this.setData({
            useArr: value
        })

        //检查是否刷新页面
        var reoladData = wx.getStorageSync('reoladPage');
        if (reoladData) {
            if (reoladData['home'] == true) {
                // this.getDataAction();
                this.getInHand();
                reoladData['home'] = false;
                wx.setStorageSync('reoladPage', reoladData);

            }
        }

    },
    onLoad: function (options) {
        if (app.user.token.length > 0) {
            // this.getDataAction();
            this.getInHand()

            var value = wx.getStorageSync("common_use") || []
            this.setData({
                useArr: value
            })

        }

    },
    checkShowAlert() {
        if (app.user.token.length > 0) {
            //判断一下
            let openId = app.user.userInfo.openId;
            let showAlert = app.user.userInfo.showAlert;
            var that = this;
            if (openId.length == 0 && showAlert == true) {
                // 绑定
                app.user.userInfo.showAlert = false;
                wx.showModal({
                    title: '微信绑定提醒',
                    content: '你还没有绑定微信，绑定微信后可接收微信消息提醒。',
                    cancelText: "绑定微信",//默认是“取消”
                    confirmText: "下次绑定",
                    success: function (res) {
                        if (res.confirm) {

                            // console.log('用户点击确定')
                        } else if (res.cancel) {
                            that.bindWxAction();
                        }
                    }
                })
            }
        }
    },
    onPullDownRefresh() {
        if (app.user.token.length > 0) {
            // this.getDataAction();
            this.getInHand()
        }
    },
    // async getDataAction() {
    //     const response = await this.$_server("home/categoryTree", {
    //         params: {},
    //     });
    //     let {data} = response;
    //     if (data.code != 200) {
    //         return;
    //     }
    //     try {
    //         let value = data.data[0].children;
    //         this.setData({
    //             categoryItems: value
    //         })
    //     } catch (e) {

    //     }
    // },
    async getInHand() {
        const response = await this.$_server("home/inHand", {});
        let {data} = response;

        if (data.code != 200) {
            return;
        }

        let value = data.data;

        let undoDescItmes = this.data.undoDescItmes;

        //判断角色
        let orgCategoryId = "";
        try {
            orgCategoryId = app.user.userInfo.orgCategoryId;
            if (orgCategoryId == 7) {
                value["unFillCount"] = '0'
                value["duplicateCount"] = '0'
            } else if (orgCategoryId == 9) {
                value["unApproveCount"] = '0'
            }
        } catch (e) {
            console.log(e);
        }

        undoDescItmes[0] = value["unFillCount"] + "个";
        undoDescItmes[1] = value["unApproveCount"] + "个";
        undoDescItmes[2] = value["duplicateCount"] + "个";
        this.setData({
            undoDescItmes: undoDescItmes
        })

    },
    onClick(e) {
        const index = e.detail.index;
        this.setData({
            "clickIndex": index
        })
    },
    handleJump(e) {
        const url = e.currentTarget.dataset.url
        wx.navigateTo({
            url: url
        })
    },
    clickUndoItem(e) {
        const url = e.currentTarget.dataset.path
        const index = e.currentTarget.dataset.index
        if (app.user.userInfo.orgCategoryId === '9' && index == 1 || app.user.userInfo.orgCategoryId === '7' && index == 2) {
            return false
        }
        if (url === "/pages/task-home/index") {
            wx.switchTab({
                url: url
            })
        } else {
            wx.navigateTo({
                url: url
            })
        }
    },
    onReady: function () {
        //  页面初次渲染完成后，使用选择器选择组件实例节点，返回匹配到组件实例对象
        this.myComponent = this.selectComponent('#myComponent')
    },
    //登录成功回调
    loginSuccess() {
        this.setData({
            user: app.user,
        });
        // this.getDataAction();
        this.getInHand();
        this.checkShowAlert();
    },
    //分享
    onShareAppMessage: function (e) {//点击button分享按钮触发此分享事件
        const item = e.target.dataset.value;
        const userInfo = (app.user && app.user.userInfo) || {};
        return {
            title: '表单填报：' + item.formName,
            path: "/pages/home/collection/collection-details/index?formId=" + item.formId + "&userId=" + userInfo.id + "&type=sb_share" + "&cityCode=" + userInfo.cityCode + "&departmentId=" + userInfo.departmentId + "&governmentId=" + userInfo.governmentId + "&orgCategoryId=" + userInfo.orgCategoryId,
            success: function (res) {
                // 转发成功
                wx.showToast({
                    title: "分享成功",
                    icon: 'success',
                    duration: 2000
                })
            },
            fail: function (res) {
                // 分享失败
                wx.showToast({
                    title: "分享失败",
                    icon: 'success',
                    duration: 2000
                })
            },
        }
    },
    reloadUse: function (e) {
        let useArr = e.detail.arr;
        this.setData({
            useArr: useArr
        })
    },
    toCall() {
        wx.makePhoneCall({
            phoneNumber: '400-8618-890 '
        })
    },
    toHelp() {
        wx.navigateTo({
            url: '/pages/help-doc/index'
        })
    }
})
